﻿using Abp.Configuration;
using Abp.Dependency;
using Abp.Runtime.Validation;
using AbpProjectTemplate.Domain.App;
using AbpProjectTemplate.Extensions;

namespace AbpProjectTemplate.Application.Filters
{
    public enum SortDirection
    {
        Asc = 0,
        Desc
    }

    public class PagedRequestFilter : IShouldNormalize
    {
        //public ISettingManager SettingManager { get; set; }

        public const int DefaultSize = 20;

        //[Range(1, 10000)]
        public int Page { get; set; }

        //[Range(1,100)]
        public int Size { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortField { get; set; }
        /// <summary>
        /// 排序类型
        /// </summary>
        public SortDirection SortDirection { get; set; } = SortDirection.Desc;

        public void Normalize()
        {
            if (this.Page <= 0)
                this.Page = 1;
            if (this.Size <= 0)
            {
                var appSettings = IocManager.Instance.Resolve<AppSettings>();
                this.Size = appSettings.PageSize;
            }
        }
        public void SetDefaultSortIfNot(string sortField, SortDirection? sortDirection = null)
        {
            if (!this.SortField.HasValue())
            {
                this.SortField = sortField;
                this.SortDirection = sortDirection ?? this.SortDirection;
            }
        }
    }

    public class KeywordsPagedRequestFilter : PagedRequestFilter
    {
        public string Keywords { get; set; }
    }
}

